﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Autofac.Core;
using Autofac.Configuration;
using Autofac.Builder;
using DemoAutofac.Models;
using AutofacContrib;
using AutofacContrib.DynamicProxy2;
using Castle.Core;
using Castle.Core.Interceptor;
using Castle.DynamicProxy;

namespace DemoAutofac {
    [Intercept("Log")]
    public interface IFoo {
        void DoSomething();
        void DoSomeMore();
    }

    // Co the dat [Intercept("Log")] o day
    public class Foo : IFoo {
        public virtual void DoSomething() {
            Console.WriteLine("In Method");
        }

        public void DoSomeMore() {
            Console.WriteLine("Do Some More");
        }
    }

    

    public class demoAOP {
        public void demoInterceptor() {
            var builder = new ContainerBuilder();

            builder.Register(c => new LogInterceptor(c.Resolve<ILogger>())).Named<IInterceptor>("Log");
            //builder.Register(c => new LogInterceptor(c.Resolve<ILogger>()));
            builder.RegisterType<Logger>().As<ILogger>().SingleInstance();

            builder.RegisterType<Foo>().As<IFoo>().EnableInterfaceInterceptors();
            //builder.RegisterType<Foo>().As<IFoo>().EnableClassInterceptors();




            IContainer container = builder.Build();

            IFoo foo = container.Resolve<IFoo>();
            foo.DoSomething();
            foo.DoSomeMore();
        }
    }
}
